// components/bottomControl/bottomControl.js
import request from '../../utils/request'
//获取全局实例
const appinstance = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    displayMusicList:[],//音乐列表
    musicid:'',//音乐id
    isplay:'',//播放状态
    musicindex:0,//音乐索引
    musicurl:'',//音乐url
    animationstate:false,//播放动画
    showList: 73, //音乐列表移动距离
    duration:'',//总时长
    currentTime:'',//实时时长
    rightDeg:'',//右边旋转
    leftDeg:'',//左边旋转


  },
  pageLifetimes: {
     // 页面被展示
    show: function () {
    //获取音乐实例
      if (wx.getStorageSync('playingMusicList').length!=0){
        let { displayMusicList, musicid, isplay, musicindex } = this.data
        musicid = appinstance.musicdata.musicid,
        isplay = appinstance.musicdata.musicisplay,
        musicindex = appinstance.musicdata.musicindex
        displayMusicList = wx.getStorageSync('playingMusicList'),
        console.log(musicid)
        console.log(isplay)
        console.log(displayMusicList)
         this.setData({
          musicid,
          isplay,
          displayMusicList,
          musicindex,
          
        })

       //获取音乐实例  监听音乐的播放、暂停、停止
    this.BackgroundAudioManager = wx.getBackgroundAudioManager();
        this.BackgroundAudioManager.onPlay(() => {
      // appinstance.musicdata.musicid = options.songid

      this.changeplay(true)
    });
        this.BackgroundAudioManager.onPause(() => {
      this.changeplay(false)
    });
        this.BackgroundAudioManager.onStop(() => {
      this.changeplay(false)
    });
    //监听背景音频自然播放结束事件
    this.BackgroundAudioManager.onEnded(() => {
      //修改当前播放的index
      let musicindex = this.data.musicindex
      this.setData({
        musicindex: musicindex+1
      })
      //调用获取音乐实例
      this.handMusicSwitch(musicindex)
      //修改实时播放时长
      // this.setData({
      //   currtimer: 0,
      //   realtime: '00:00'
      // })
    });

        //获取实时播放时间
        this.BackgroundAudioManager.onTimeUpdate(() => {
          let { duration, currentTime, leftDeg, rightDeg} = this.data
           duration = this.BackgroundAudioManager.duration
           currentTime = this.BackgroundAudioManager.currentTime

          // 右侧半圆在进度超过一半之后要保持旋转225deg状态,未超过一半，左侧半圆保持原始角度45deg
          if (currentTime / duration <= 0.5) {
            leftDeg = '45deg'
            rightDeg = currentTime / duration * 360 + 45 + 'deg'
            // visible = 'hidden'
          } else {
            leftDeg = currentTime / duration * 360 + 225 + 'deg'
            rightDeg = '225deg'
            // visible = 'visible'
          }


          this.setData({
            leftDeg,
            rightDeg,
            duration,
            currentTime
          })

        })



     
    }
     

      
    },
    // 页面被隐藏
    hide: function () {
     
    },
     // 页面被隐藏
    resize: function () {
   
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //修改播放状态
    changeplay(isplay) {
      this.setData({
        isplay
      })
      appinstance.musicdata.musicisplay = isplay;
    },
    // 处理音乐的播放与暂停
    handMusicPlay() {
      let { displayMusicList, isplay, musicindex } = this.data
      this.BackgroundAudioManager = wx.getBackgroundAudioManager();
      if (displayMusicList.length <= 0) {
        return
      }
      if (isplay) {
        // console.log(111111111111);
        this.BackgroundAudioManager.pause()
        // console.log(this.BackgroundAudioManager.src)
      } else {
        if (this.data.musicurl) {
          this.BackgroundAudioManager.play()
        } else {
          this.handMusicSwitch(musicindex)
          console.log(2222222)
        }
      }
      isplay = !isplay
      this.setData({
        isplay,
        animationstate: true
    
      })
      appinstance.musicdata.isplay = isplay;
    },
    
    //切换歌曲
    changemusicswitch(event){
      this.setData({
        animationstate:false
      })
      console.log(event.detail.current)
      let { displayMusicList, musicid, isplay, musicindex, musicurl} = this.data
      musicindex = event.detail.current
      musicid = displayMusicList[musicindex].id
      this.setData({
        musicindex,
        musicid,
        isplay:false,
        musicurl:'',
        animationstate: true
      })
     
      //调用音乐实例
      this.handMusicPlay();
    },

    //获取音乐实例
    async handMusicSwitch(current){
      let { musicurl} = this.data
     //获取歌曲url
   let res = await request('/song/url', {
     id: this.data.displayMusicList[current].id
     })
        //  console.log(res)
  
         //获取音频实例
      this.BackgroundAudioManager = wx.getBackgroundAudioManager();
      // console.log(this.BackgroundAudioManager.src)
      this.BackgroundAudioManager.title = this.data.displayMusicList[current].name;
      this.BackgroundAudioManager.src = res.data[0].url;
      //将音乐链接存储，减少请次数
      musicurl = res.data[0].url
      this.setData({
        musicurl
      })
      // console.log(res.data[0].url)
      // console.log(this.BackgroundAudioManager.src)
      
    },

    // 显示歌单列表
    showMusicList() {
      this.setData({
        showList: 0
      })
    },

    // 点击遮罩层 隐藏歌单列表
    hideMusicList() {
      this.setData({
        showList: 73
      })
    },

    //音乐列表点击播放歌曲
    handplaymusic(event) {
      let {
        itemmusicindex,
        itemmusicid
      } = event.currentTarget.dataset
  
      let { musicid, musicindex} = this.data
      if (musicid != itemmusicid.toString()) {

        //将number转化为string
        musicid = itemmusicid.toString()
        musicindex = itemmusicindex
        this.setData({
         
          musicurl: '', //音乐链接
          isplay: false,
          musicindex,
          musicid
        })
        //更新app全局播放列表当前index
        appinstance.musicdata.musicindex = itemmusicindex;
        this.handMusicSwitch(itemmusicindex);

      } else {
        this.setData({
          isplay: !this.data.isplay
        })
        this.handMusicPlay()

      }


    },
    //跳转到播放音乐
    async playmusic(event) {
      let { songid, index } = event.currentTarget.dataset;
      let { displayMusicList,musicindex} = this.data
      this.setData({
        musicindex:index
      })
      wx.setStorageSync('playingMusicList', displayMusicList);
      wx.navigateTo({
        url: '/pages/songdetail/songdetail?songid=' + songid,
        // success: (res) => {
        //   // 通过eventChannel向被打开页面传送数据,这个data名字是你自己取的任意,在目标页面中有个参数接收就可以
        //   res.eventChannel.emit('parentPageEmit', { data: index });

        // },

      })
    },
    

  }
})
